package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBundler();
        btn1Event();
        btn2Event();
        btn3Event();
        btn4Event();
        btn5Event();
        btn6Event();



    }

    private void btn6Event() {
             btn6.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     String weatherStr="{\n" +
                             "\t\"time\": \"2019-03-25 09:00:00\",\n" +
                             "\t\"cityInfo\": {\n" +
                             "\t\t\"city\": \"广州市\",\n" +
                             "\t\t\"cityId\": \"101280101\",\n" +
                             "\t\t\"parent\": \"广东\",\n" +
                             "\t\t\"updateTime\": \"08:39\"\n" +
                             "\t},\n" +
                             "\t\"date\": \"20190325\",\n" +
                             "\t\"message\": \"Success !\",\n" +
                             "\t\"status\": 200,\n" +
                             "\t\"data\": {\n" +
                             "\t\t\"shidu\": \"99%\",\n" +
                             "\t\t\"pm25\": 5.0,\n" +
                             "\t\t\"pm10\": 6.0,\n" +
                             "\t\t\"quality\": \"优\",\n" +
                             "\t\t\"wendu\": \"13\",\n" +
                             "\t\t\"ganmao\": \"各类人群可自由活动\",\n" +
                             "\t\t\"yesterday\": {\n" +
                             "\t\t\t\"date\": \"24\",\n" +
                             "\t\t\t\"sunrise\": \"06:29\",\n" +
                             "\t\t\t\"high\": \"高温 17.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 12.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:39\",\n" +
                             "\t\t\t\"aqi\": 15.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-24\",\n" +
                             "\t\t\t\"week\": \"星期日\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"中雨\",\n" +
                             "\t\t\t\"notice\": \"记得随身携带雨伞哦\"\n" +
                             "\t\t},\n" +
                             "\t\t\"forecast\": [{\n" +
                             "\t\t\t\"date\": \"25\",\n" +
                             "\t\t\t\"sunrise\": \"06:28\",\n" +
                             "\t\t\t\"high\": \"高温 19.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 14.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:40\",\n" +
                             "\t\t\t\"aqi\": 18.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-25\",\n" +
                             "\t\t\t\"week\": \"星期一\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"小雨\",\n" +
                             "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"26\",\n" +
                             "\t\t\t\"sunrise\": \"06:27\",\n" +
                             "\t\t\t\"high\": \"高温 24.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 16.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:40\",\n" +
                             "\t\t\t\"aqi\": 69.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-26\",\n" +
                             "\t\t\t\"week\": \"星期二\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"27\",\n" +
                             "\t\t\t\"sunrise\": \"06:26\",\n" +
                             "\t\t\t\"high\": \"高温 26.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 20.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:41\",\n" +
                             "\t\t\t\"aqi\": 52.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-27\",\n" +
                             "\t\t\t\"week\": \"星期三\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"28\",\n" +
                             "\t\t\t\"sunrise\": \"06:25\",\n" +
                             "\t\t\t\"high\": \"高温 27.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 20.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:41\",\n" +
                             "\t\t\t\"aqi\": 55.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-28\",\n" +
                             "\t\t\t\"week\": \"星期四\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"29\",\n" +
                             "\t\t\t\"sunrise\": \"06:24\",\n" +
                             "\t\t\t\"high\": \"高温 25.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 18.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:41\",\n" +
                             "\t\t\t\"aqi\": 52.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-29\",\n" +
                             "\t\t\t\"week\": \"星期五\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"小雨\",\n" +
                             "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"30\",\n" +
                             "\t\t\t\"sunrise\": \"06:23\",\n" +
                             "\t\t\t\"high\": \"高温 25.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 19.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:42\",\n" +
                             "\t\t\t\"aqi\": 57.0,\n" +
                             "\t\t\t\"ymd\": \"2019-03-30\",\n" +
                             "\t\t\t\"week\": \"星期六\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"31\",\n" +
                             "\t\t\t\"sunrise\": \"06:22\",\n" +
                             "\t\t\t\"high\": \"高温 24.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 18.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:42\",\n" +
                             "\t\t\t\"ymd\": \"2019-03-31\",\n" +
                             "\t\t\t\"week\": \"星期日\",\n" +
                             "\t\t\t\"fx\": \"无持续风向\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"01\",\n" +
                             "\t\t\t\"sunrise\": \"06:21\",\n" +
                             "\t\t\t\"high\": \"高温 26.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 18.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:42\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-01\",\n" +
                             "\t\t\t\"week\": \"星期一\",\n" +
                             "\t\t\t\"fx\": \"东北风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"02\",\n" +
                             "\t\t\t\"sunrise\": \"06:20\",\n" +
                             "\t\t\t\"high\": \"高温 26.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 18.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:43\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-02\",\n" +
                             "\t\t\t\"week\": \"星期二\",\n" +
                             "\t\t\t\"fx\": \"东南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"晴\",\n" +
                             "\t\t\t\"notice\": \"愿你拥有比阳光明媚的心情\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"03\",\n" +
                             "\t\t\t\"sunrise\": \"06:19\",\n" +
                             "\t\t\t\"high\": \"高温 27.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 20.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:43\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-03\",\n" +
                             "\t\t\t\"week\": \"星期三\",\n" +
                             "\t\t\t\"fx\": \"南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"小雨\",\n" +
                             "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"04\",\n" +
                             "\t\t\t\"sunrise\": \"06:18\",\n" +
                             "\t\t\t\"high\": \"高温 30.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 22.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:43\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-04\",\n" +
                             "\t\t\t\"week\": \"星期四\",\n" +
                             "\t\t\t\"fx\": \"南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"05\",\n" +
                             "\t\t\t\"sunrise\": \"06:18\",\n" +
                             "\t\t\t\"high\": \"高温 31.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 22.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:44\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-05\",\n" +
                             "\t\t\t\"week\": \"星期五\",\n" +
                             "\t\t\t\"fx\": \"南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"06\",\n" +
                             "\t\t\t\"sunrise\": \"06:17\",\n" +
                             "\t\t\t\"high\": \"高温 30.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 22.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:44\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-06\",\n" +
                             "\t\t\t\"week\": \"星期六\",\n" +
                             "\t\t\t\"fx\": \"南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"多云\",\n" +
                             "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"07\",\n" +
                             "\t\t\t\"sunrise\": \"06:16\",\n" +
                             "\t\t\t\"high\": \"高温 29.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 23.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:45\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-07\",\n" +
                             "\t\t\t\"week\": \"星期日\",\n" +
                             "\t\t\t\"fx\": \"东南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"小雨\",\n" +
                             "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
                             "\t\t}, {\n" +
                             "\t\t\t\"date\": \"08\",\n" +
                             "\t\t\t\"sunrise\": \"06:15\",\n" +
                             "\t\t\t\"high\": \"高温 29.0℃\",\n" +
                             "\t\t\t\"low\": \"低温 23.0℃\",\n" +
                             "\t\t\t\"sunset\": \"18:45\",\n" +
                             "\t\t\t\"ymd\": \"2019-04-08\",\n" +
                             "\t\t\t\"week\": \"星期一\",\n" +
                             "\t\t\t\"fx\": \"东南风\",\n" +
                             "\t\t\t\"fl\": \"<3级\",\n" +
                             "\t\t\t\"type\": \"小雨\",\n" +
                             "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
                             "\t\t}]\n" +
                             "\t}\n" +
                             "}";
                     Gson gson=new Gson();

                     weather weather=gson.fromJson(weatherStr, weather.class);

                     String type=weather.getData().getForecast().get(0).getType();
                     String low=weather.getData().getForecast().get(0).getLow();
                     String high=weather.getData().getForecast().get(0).getHigh();


                     Log.e(TAG, " type"+type +"low:"+low +" high   "+high);



                 }
             });
    }

    private void btn5Event() {
           btn5.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   String animal ="{\n" +
                           "\t\"animals\":{\n" +
                           "\t\"dog\":[\n" +
                           "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
                           "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
                           "\t],\n" +
                           "\t\"cat\":{\"name\":\"Matilda\"}\n" +
                           "}\n" +
                           "}";

                   Gson gson=new Gson();

                   AnimalData animalData=gson.fromJson(animal,AnimalData.class);

                   String dogname=animalData.getAnimals().getDog().get(0).getName();
                   String catname=animalData.getAnimals().getCat().getName();
                   Log.e(TAG, "dogname :"+dogname+"   catname:"+catname );



               }
           });
    }

    private void btn4Event() {
                btn4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String animal ="{\n" +
                                "\t\"animals\":{\n" +
                                "\t\"dog\":[\n" +
                                "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
                                "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
                                "\t],\n" +
                                "\t\"cat\":{\"name\":\"Matilda\"}\n" +
                                "}\n" +
                                "}";
            Gson gson=new Gson();

            JsonRootBean jsonRootBean=gson.fromJson(animal,JsonRootBean.class);

            String dog1=jsonRootBean.getAnimals().getDog().get(0).getName();
            String cat1=jsonRootBean.getAnimals().getCat().getName();

                        Log.e(TAG, "dog1:"+dog1+" cat1 :"+cat1 );



                    }
                });
    }

    private void btn3Event() {
              btn3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      String str="{\"name\":\"张三\",\"age\":20}";
                      Gson gson=new Gson();
                      Student student=gson.fromJson(str,Student.class);
                      Log.e(TAG, "student :"+student.toString() );

                  }
              });
    }

    private void btn2Event() {
           btn2.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   Gson gson=new Gson();

                   Student student=new Student("张三",20);
                   String str=gson.toJson(student);

                   Log.e(TAG, "str ="+str );
               }
           });
    }

    private void btn1Event() {

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Gson gson=new Gson();

                  String str1=gson.toJson("hello");
                  String str2=gson.toJson("word");
                  String str3=gson.toJson(false);
                  String [] arr={"hello","world","welcome"};
                  String str4=gson.toJson(arr);

                Log.e(TAG, "str1 ="+str1+"str2 ="+str2
                +"str3 ="+str3 +" str4 ="+str4);

            }
        });

    }

    private void initBundler() {
                 btn1=findViewById(R.id.btn1);
                 btn2=findViewById(R.id.btn2);
                 btn3=findViewById(R.id.btn3);
                 btn4=findViewById(R.id.btn4);
                 btn5=findViewById(R.id.btn5);
                 btn6=findViewById(R.id.btn6);
    }

}
